<?php
class articleController extends adminController{
    protected $layout = 'layout';
    //文章列表
    public function lists(){
        $act = in($_REQUEST['act']);
        if ($act == "alldel") {//批量删除
            $fid = $_POST['fid'];
            if (is_array($fid)) {
                foreach ($fid as $value) {//开始删除
                    $id  = explode('_',$value);
                    $rel = model('article')->delInfo($id[0],$id[1]); 
                }
                $this->pageMsg('已成功删除！请返回');
            }else{
                $this->pageMsg('没有选择任何要删除的内容');
            }
        }elseif($act == 'del'){
            $cid = intval($_GET['cid']);
            $fid = intval($_GET['id']);
            $rel = model('article')->delInfo($cid,$fid); 
            if($rel){
                echo 1;
            }else{
                echo '删除失败';
            }
        }else{
            $this->fid = intval($_GET['id']);
            if (!$this->fid) {
                $this->pageError();
            }
            $all = model('channel')->AllChildClass($this->fid);
            $listRows = 20;
            $url = url('article/lists',array('id'=>$this->fid,'page'=>'{page}'));
            $allcount = model('article')->counts($this->fid,$all);
            $limit = $this->pageLimit($url,$listRows);
            $this->page= $this->pageShow($allcount);
            $this->path = model('channel')->flpath($this->fid,'article/lists');
            $this->list = model('article')->lists($this->fid,$all,$limit);
            $this->display('article/lists');
        }
    }
    //添加内容
    public function add(){
        $this->cid = intval($_GET['id']);
        if (!$this->cid) {
            $this->pageError();
        }
        //获取根ID
        $root = model('channel')->find('id='.$this->cid,'id,pid,rootid,mid');
        if (!$root) {
            $this->pageError();
        }
        if($root['pid'] == 0){
            $this->rootid = $root['id'];
        }else{
            $this->rootid = $root['rootid'];
        }
        $this->mid = $root['mid'];
        $this->editor = $_SESSION[$this->appID . '_userInfo']['username'];
        $this->path = model('channel')->flpath($this->cid,'article/lists'); //当前路径
        $this->module = model('article')->addInput($this->mid); 
        $this->display('article/add');
    }
    //编辑内容
    public function edit(){
        $this->fid = intval($_GET['id']);
        $this->cid = intval($_GET['cid']);
        $msg = Check::rule(array(check::must($this->cid),'栏目ID出错'),array(check::must($this->fid),'ID参数出错'));
        if(true !== $msg){
            cpError::show($msg);
        }
        $root = model('channel')->find('id='.$this->cid,'id,pid,rootid,mid');
        if (!$root) {
            $this->pageError();
        }
        if($root['pid'] == 0){
            $this->rootid = $root['id'];
        }else{
            $this->rootid = $root['rootid'];
        }
        $this->mid = $root['mid'];
        $this->info=model('article')->editInfo($this->cid,$this->fid);
        if (!$this->info) {
            $this->pageError();
        }
        $this->module = model('article')->editInput($this->mid,$this->info); 
        $this->path = model('channel')->flpath($this->cid,'article/lists');
        $this->display('article/edit');
    }
    //保存
    public function save(){
        if($this->isPost()){
            $fid = intval($_POST['fid']);
            $cid = intval($_POST['cid']); 
            $mid = intval($_POST['mid']);
            $_POST['uptime'] = time();
            $post_array = postinput($_POST,array('fid','mid'));
            $rel = model('article')->dataSave($mid,$post_array,$fid);
            $this->redirect(url('article/lists',array('id'=>$cid)));
        }
    }
    //状态更改
    public function state(){
        $cid = intval($_GET['cid']);
        $fid = intval($_GET['fid']);
        $state = $_GET['state'];
        $type = $_GET['type'];
        $rel = model('article')->states($type,$cid,$fid,$state);
        if($rel){
            echo 'ok';
        }
    }
    //扩展列表内容
    public function extlists(){
        $act = in($_REQUEST['act']);
        if ($act == "alldel") {//批量删除
            $mid = $_POST['mid'];
            $cid = $_POST['cid'];
            $fid = $_POST['fid'];
            $aid = $_POST['aid'];
            $title = $_POST['title'];
            $aid = $_POST['aid'];
            if (is_array($aid)) {
                foreach ($aid as $value) {//开始删除
                    $rel = model('article')->extdelInfo($mid,$value); 
                }
                $this->pageMsg('已成功删除！请返回');
            }else{
                $this->pageMsg('没有选择任何要删除的内容');
            }
        }elseif($act == 'del'){
            $mid = $_GET['mid'];
            $aid = $_GET['aid'];
            $rel = model('article')->extdelInfo($mid,$aid); 
            if($rel){
                echo 1;
            }else{
                echo '删除失败';
            }
        }else{
            $this->mid = intval($_GET['mid']);
            $this->cid = intval($_GET['cid']);
            $this->fid = intval($_GET['fid']);
            $this->title = $_GET['title'];
            if (!$this->mid || !$this->cid || !$this->fid || !$this->title) {
                $this->pageError();
            }
            $this->list = model('article')->extlists($this->mid,$this->fid);
            $this->path = model('channel')->flpath($this->cid,'article/lists');
            $this->display('article/lists-exts');
        }
    }
    //添加和编辑扩展
    public function extsadd(){
        $this->mid = $_GET['mid'];
        $this->cid = $_GET['cid'];
        $this->fid = $_GET['fid'];
        $this->aid = $_GET['aid'];
        $this->title = $_GET['title'];
        if(!$this->mid || !$this->cid || !$this->fid || !$this->title){
            $this->pageError();
        }
        $this->path = model('channel')->flpath($this->cid,'article/lists');
        if($this->aid){
            $this->info = model('article')->extseditInfo($this->mid,$this->aid);
            if($this->info){
                $this->module = model('article')->editInput($this->mid,$this->info); 
            }  
        }else{
            $this->module = model('article')->addInput($this->mid); 
        }
        $this->display('article/edit-exts');
    }
    //保存
    public function extssave(){
        $mid = $_POST['mid'];
        $cid = $_POST['cid'];
        $fid = $_POST['fid'];
        $aid = $_POST['aid'];
        $title = $_POST['title'];
        $data = postinput($_POST,array('mid','cid','fid','aid','title','exttitle'));
        $data['title'] = $_POST['exttitle'];
        $data['fontcolor'] = $_POST['fontcolor'];
        $data['fid'] = $_POST['fid'];
        $data['cid'] = $_POST['cid'];
        $rel = model('article')->extsSave($mid,$data,$aid);
        $this->redirect(url('article/extlists',array(mid=>$mid,cid=>$cid,fid=>$fid,title=>$title)));
    }
}